// Create a new Critter review assignment
// Mabel Xu, April 29 2008

import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.actor.Rock;
import info.gridworld.grid.Location;

import java.util.ArrayList;

public class HalfCritter extends Critter
{
	public ArrayList<Actor> getActors()
	{
		ArrayList<Actor> neighbors = this.getGrid().getNeighbors(this.getLocation());
		ArrayList<Actor> acts = new ArrayList<Actor>();
		for (int i = neighbors.size() - 1; i >= 0; i--)
		{
			if (neighbors.get(i) instanceof Rock)
			{
				acts.add(neighbors.get(i));
				neighbors.remove(i);
			}
		}
		int eats = neighbors.size() / 2;
		for (int i = eats - 1; i >= 0; i--)
		{
			acts.add(neighbors.get(i));
			neighbors.remove(i);
		}
		return acts;
	}
	
	public void processActors(ArrayList<Actor> actors)
	{
		for (Actor a : actors)
			a.removeSelfFromGrid();
	}
	
	public ArrayList<Location> getMoveLocations()
	{
		ArrayList<Location> locs = this.getGrid().getEmptyAdjacentLocations(this.getLocation());;
		for (int i = locs.size() - 1; i >= 0; i--)
			if (this.getGrid().getNeighbors(locs.get(i)).size() == 0)
				locs.remove(i);
		return locs;
	}
	
	public Location selectMoveLocation(ArrayList<Location> locs)
	{
		if (locs.size() != 0)
		{
			int r = (int)(Math.random() * locs.size());
			return locs.get(r);
		}
		else
		{
			Location north = this.getLocation().getAdjacentLocation(Location.NORTH);
			if (this.getGrid().isValid(north) && this.getGrid().get(north) == null)
				return north;
			else
				return this.getLocation();
		}
	}
	
	public void makeMove(Location loc)
	{
		if (loc.equals(this.getLocation()))
			this.setDirection(this.getDirection() + Location.HALF_CIRCLE);
		else
			moveTo(loc);
	}
}

// the Critter class act() is never overwritten (and it wasn't, in this class)
// all 5 steps of the Critter class were overwritten, including getActors(), processActors(), getMoveLocations(), selectMoveLocation(), and makeMove()